"""Plot Affy Data

.. helpdoc::
Plots a summary of affy data.

"""

"""<widgetXML>
    <name>Plot Affy Data</name>
    <icon>default.png</icon>
    <tags>
        <tag>Affy</tag>
    </tags>
    <summary>Plots a summary of affy data.  Also this widget has simple interfaces to display expression boxplots, and do simple quality control.</summary>
    <author>
        <authorname>Kyle R Covington</authorname>
        <authorcontact>kyle@red-r.org</authorcontact>
    </author>
</widgetXML>
"""

"""
<name>Plot Affy Image</name>
<description>Obtains an affybatch and plots the images of the files</description>
<tags>Affy QC</tags>
<RFunctions>affy:image</RFunctions>
<icon>plotaffy.png</icon>
"""

from OWRpy import *
import redRGUI
import signals
class plotAffy(OWRpy):
    settingsList = ['irows', 'icols', 'qcsProcessed']
    def __init__(self, **kwargs):
        OWRpy.__init__(self, **kwargs)
        self.require_librarys(['affy', 'simpleaffy'])
        self.setRvariableNames(['qcs'])
        self.qcsProcessed = 0
        self.data = ''
        
        self.saveSettingsList.extend(['qcsProcessed', 'data'])

        """.. rrsignals::"""
        self.inputs.addInput('ab', "Affybatch", signals.affy.RAffyBatch, self.init)
        
        #the GUI
        info = redRGUI.base.widgetBox(self.controlArea, "Info")
        #self.infoa = redRGUI.widgetLabel(info, 'No data loaded.')
        redRGUI.base.button(info, "Show Image", callback = self.process, width = 150)
        redRGUI.base.button(info,"Show Box plot", callback = self.myboxplot, width = 150)
        redRGUI.base.button(info, "Process and Show QC", callback = self.RAffyQC, width = 150)
        
        optionsa = redRGUI.base.groupBox(self.controlArea, "Options")
        #testlineButton = redRGUI.button(optionsa, self, "test line edit", callback = self.test, width = 200)
        
        self.plotArea = redRGUI.base.graphicsView(self.controlArea, label = 'Plot')
        
        
    def loadCustomSettings(self,settings=None):
        print 'load affy plot'
        self.processSignals()
        
    def init(self, dataset):
        if dataset:
            self.data = str(dataset.getData())
            self.status.setText("Data Connected")
            self.qcsProcessed == 0
        else:
            self.status.setText("No data loaded or not of appropriate type.")
            self.data = ''
    
    def process(self):
        if self.data != '':
            self.plotArea.plot(function = 'image', query = self.data)
        else:
            self.status.setText("No data connected.")
    def myboxplot(self):
        if self.data == '':return
        #try:
        self.plotArea.plot(function = 'boxplot', query = self.data)
        #except:     
        #    self.infob.setText("Data not able to be processed")
        
    def RAffyQC(self):
        if self.data =='': return
        if self.qcsProcessed == 0:
            self.R('%(new)s<-qc(%(data)s)' % {'new':self.Rvariables['qcs'], 'data':str(self.data)}, wantType = 'NoConversion')
        self.plotArea.plot(self.Rvariables['qcs'])
        self.qcsProcessed = 1
        